
package teehee.DB;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;

import teehee.homepage.R;

import android.content.Context;
import android.content.res.Resources;

/**
 * @author Shub
 * 
 * Eric: Are you sure this will work?
 * Shub: I HAVE NO IDEA!!!
 *
 */
public class DB {
private String type;
private String name;
private String reps;
private String time;
private String weights;
private String comments;
private static ArrayList<DB> db_wodlist = new ArrayList<DB>();
private static ArrayList<DB> db_exlist = new ArrayList<DB>();
private ArrayList<String> exList;


/**
 * Private c'tor to instantiate a MockDB object with following fields.
 * @param  type wod/ex
 * @param  name name of the wod/ex
 * @param  reps reps of the ex
 * @param  time time taken to complete
 * @param  weights weights used
 * @param  comments desc/comments on the wod/ex
 * @param  exercises list of ex in wod/null otherwise
 * @return 
 * @see
*/

private DB(String type, String name, String reps, String time, String weights, String comments, ArrayList<String> exList)
{
	this.type = type;
	this.name = name;
	this.reps = reps;
	this.time = time;
	this.weights = weights;
	this.comments = comments;
	this.exList = exList;
}


/**
 * Method to return the type.
 * @param  none
 * @return String
 * @see
*/

public String getType()
{
	return this.type;
}


/**
 * Method to return the name.
 * @param  none
 * @return String
 * @see
*/

public String getName()
{
	return this.name;
}


/**
 * Method to return the reps.
 * @param  none
 * @return String
 * @see
*/

public String getReps()
{
	return this.reps;
}


/**
 * Method to return the time.
 * @param  none
 * @return String
 * @see
*/

public String getTime()
{
	return this.time;
}



/**
 * Method to return the weights.
 * @param  none
 * @return String
 * @see
*/

public String getWeights()
{
	return this.weights;
}



/**
 * Method to return the comments.
 * @param  none
 * @return String
 * @see
*/

public String getComments()
{
	return this.comments;
}



/**
 * Method to return the list of exercises.
 * @param  none
 * @return ArrayList<String>
 * @see
*/
public ArrayList<String> getExerciseList()
{
	return this.exList;
}

@SuppressWarnings("unused")
private static void readWodFile(BufferedReader buffreader) throws FileNotFoundException
{
	try{
	DB entry;
	String name,reps,time,weight, comments;
	ArrayList<String> exList = new ArrayList<String>();
	while((name = buffreader.readLine())!= null)
	{
		String token;
		while(!(token = buffreader.readLine()).equals("-"))
		{
			exList.add(token);
		}
		reps = buffreader.readLine();
		time = buffreader.readLine();
		weight = buffreader.readLine();
		comments = buffreader.readLine();
		entry = new DB("wod", name, reps,time,weight,comments,exList);
		db_wodlist.add(entry);
	}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
}

private static String readWodFileByte(FileInputStream fis) throws FileNotFoundException
{
	String db = "";
	try {
		Scanner s = new java.util.Scanner(fis);
        db = s.useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
         e.printStackTrace();
    }

	return db;
}

private static void buildWodFile(BufferedReader buffreader, FileOutputStream fos) throws FileNotFoundException
{
	try{
	String token;
	while(( token = buffreader.readLine())!= null)
	{
		fos.write(token.getBytes());
	}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
}

private static void parse(String db, String type)
{
	String token = new String(db);
	int ind1, ind2;
	int indsep;
	String name = "",reps ="",time="",comment="",wt="";
	while(token.length() > 5)
	{
		ArrayList<String> exlist = new ArrayList<String>();
		ind1 = token.indexOf(",");
		name = token.substring(0,ind1);
		token = token.substring(ind1+1);
		ind2 = token.indexOf(",");
		indsep = token.indexOf("!");
		while(ind2 < indsep)
		{
			exlist.add(token.substring(0, ind2));
			token = token.substring(ind2+1);
			ind1 = ind2;
			ind2 = token.indexOf(",");
			indsep = token.indexOf("!");
		}
		
		exlist.add(token.substring(0, indsep));
		token = token.substring(indsep+1);
		ind2 = token.indexOf(",");
		reps = token.substring(0, ind2);
		token = token.substring(ind2+1);
		ind2 = token.indexOf(",");
		time = token.substring(0, ind2);
		token = token.substring(ind2+1);
		ind2 = token.indexOf(",");
		wt = token.substring(0, ind2);
		token = token.substring(ind2+1);
		ind2 = token.indexOf("*");
		comment = token.substring(0, ind2);
		token = token.substring(ind2+1);
		if(type.equals("wod"))
		{
			DB entry = new DB("wod", name, reps, time, wt, comment, exlist);
			db_wodlist.add(entry);
		}
		else
		{
			exlist.remove(0);
			exlist.add(name);
			DB entry = new DB("ex", name, reps, time, wt,comment, exlist);
			db_exlist.add(entry); 
		}
	}
}

private static void writeToFile(Context ctx, String file, DB entry)
{
	try{
	FileOutputStream fos = ctx.openFileOutput(file, Context.MODE_APPEND);
	ArrayList<String> list = entry.getExerciseList();
	fos.write(entry.getName().getBytes());
	String delm = ",";
	String exEnd = "!";
	String wodEnd = "*";
	fos.write(delm.getBytes());
	for(int i = 0; i < list.size(); i++)
	{
		fos.write(list.get(i).getBytes());
		if(i!= list.size()-1)
		fos.write(delm.getBytes());
	}
	fos.write(exEnd.getBytes());
	fos.write(entry.getReps().getBytes());
	fos.write(delm.getBytes());
	fos.write(entry.getTime().getBytes());
	fos.write(delm.getBytes());
	fos.write(entry.getWeights().getBytes());
	fos.write(delm.getBytes());
	fos.write(entry.getComments().getBytes());
	fos.write(wodEnd.getBytes());
	fos.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

public static void createDB(Context ctx)
{
	try{
	Resources myResources = ctx.getResources();
	InputStream inputStream = myResources.openRawResource(R.raw.fwod);
	InputStreamReader inputreader = new InputStreamReader(inputStream);
	BufferedReader buffreader = new BufferedReader(inputreader);
	FileOutputStream fos = ctx.openFileOutput("fwod", Context.MODE_PRIVATE);
	buildWodFile(buffreader, fos);
	buffreader.close();
	inputreader.close();
	inputStream.close();
	fos.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

/**
 * Method to instantiate the database
 * @param  ctx context of the application
 * @return void
 * @see
*/
@SuppressWarnings("static-access")
public static void instantiateDB(Context ctx)
{
	try
	{
		db_wodlist = new ArrayList<DB>();
		db_exlist = new ArrayList<DB>();
	File f = ctx.getFileStreamPath("awod");
	if(!f.exists())
	{
		
		Resources myResources = ctx.getResources();
		FileOutputStream fos = ctx.openFileOutput("date", Context.MODE_PRIVATE);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat form = new SimpleDateFormat("MMddyyyy");
		c.add(c.DATE, -1);
		String date = form.format(c.getTime());
		fos.write(date.getBytes());
		fos.close();
		
		InputStream inputStream = myResources.openRawResource(R.raw.awod);
		InputStreamReader inputreader = new InputStreamReader(inputStream);
		BufferedReader buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("awod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.bwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("bwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.cwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("cwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.dwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("dwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.ewod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("ewod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.fwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("fwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.gwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("gwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.hwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("hwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.iwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("iwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.jwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("jwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.kwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("kwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.lwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("lwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.mwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("mwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.nwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("nwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.owod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("owod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.pwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("pwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.qwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("qwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.rwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("rwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.swod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("swod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.twod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("twod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.uwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("uwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.vwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("vwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.wwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("wwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.xwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("xwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.ywod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("ywod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.zwod);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("zwod", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.aex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("aex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.bex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("bex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.cex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("cex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.dex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("dex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.eex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("eex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.fex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("fex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.gex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("gex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.hex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("hex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.iex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("iex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.jex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("jex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.kex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("kex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.lex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("lex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.mex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("mex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.nex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("nex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.oex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("oex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.pex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("pex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.qex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("qex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.rex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("rex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.sex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("sex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.tex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("tex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.uex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("uex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.vex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("vex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.wex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("wex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.xex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("xex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.yex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("yex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		inputStream = myResources.openRawResource(R.raw.zex);
		inputreader = new InputStreamReader(inputStream);
		buffreader = new BufferedReader(inputreader);
		fos = ctx.openFileOutput("zex", Context.MODE_PRIVATE);
		buildWodFile(buffreader, fos);
		buffreader.close();
		inputreader.close();
		inputStream.close();
		fos.close();
		
		
	}
		char c = 0x61;
		while(c < 123)
		{
			String filename = c+"wod";
		FileInputStream fis = ctx.openFileInput(filename);
		String db = readWodFileByte(fis);
		parse(db, "wod");
		fis.close();
		
		filename = c+"ex";
		fis = ctx.openFileInput(filename);
		db = readWodFileByte(fis);
		parse(db, "ex");
		fis.close();
		c++;
		}
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}


/**
 * Method to create a new entry in the database
 * @param  ctx context of the application
 * @param  type wod/ex
 * @param  name name of the wod/ex
 * @param  reps reps of the ex
 * @param  time time taken to complete
 * @param  weights weights used
 * @param  comments desc/comments on the wod/ex
 * @param  exercises list of ex in wod/null otherwise
 * @return boolean
 * @see
*/

public static boolean createNewEntry(Context ctx, String type, String name, String reps, String time, String weights, String comments, ArrayList<String> exercises)
throws UnsupportedOperationException{
	boolean success = true;
	DB newEntry = new DB(type, name, reps, time, weights, comments, exercises);
	if(type.equals("ex"))
	{
		if(!name.equals("0") && reps.equals("0") && time.equals("0") && weights.equals("0") && exercises.size() == 1 && exercises.get(0).equals("0")
				&& !exists(name))
		{
			db_exlist.add(newEntry);
			String filename = name.substring(0,1)+"ex";
			writeToFile(ctx, filename, newEntry);
			return success;
		}
		else
		{
			return !success;
		}
	}
	else if(type.equals("wod"))
	{
		if( !name.equals("0") && exercises != null && exercises.size() > 0 && 
				!exists(name))
		{
			db_wodlist.add(newEntry);
			String filename = name.substring(0,1)+"wod";
			writeToFile(ctx, filename, newEntry);
			return success;
		}
		else
		{
			return !success;
		}
	}
	else
		throw new UnsupportedOperationException();
}


/**
 * Method to find a wod/ex in the database.
 * @param  name name of the wod/ex you need to find.
 * @return MockDB
 * @see
*/

public static DB find(String name)
{
	DB dummy;
	String dummyName;
	for(int i = 0; i< db_wodlist.size(); i++)
	{
		dummy = db_wodlist.get(i);
		dummyName = dummy.getName();
		if(dummyName.equals(name))
		{
			return dummy;
		}
	}
	
	for(int i = 0; i< db_exlist.size(); i++)
	{
		dummy = db_exlist.get(i);
		dummyName = dummy.getName();
		if(dummyName.equals(name))
		{
			return dummy;
		}
	}
	
	return null;
}

/**
 * Method to find if a wod/ex exists in the database.
 * @param  name name of the wod/ex you need to check for.
 * @return boolean
 * @see
*/

public static boolean exists(String name)
{
	DB dummy;
	String dummyName;
	for(int i = 0; i< db_wodlist.size(); i++)
	{
		dummy = db_wodlist.get(i);
		dummyName = dummy.getName();
		if(dummyName.equals(name))
		{
			return true;
		}
	}
	
	for(int i = 0; i< db_exlist.size(); i++)
	{
		dummy = db_exlist.get(i);
		dummyName = dummy.getName();
		if(dummyName.equals(name))
		{
			return true;
		}
	}
	
	return false;
}

public static DB findWod(String name)
{
	DB dummy;
	String dummyName;
	for(int i = 0; i< db_wodlist.size(); i++)
	{
		dummy = db_wodlist.get(i);
		dummyName = dummy.getName();
		if(dummyName.equals(name))
		{
			return dummy;
		}
	}
	
	return null;
}

}
